package com.atguigu.rabbitmq.util;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author codingBee
 * @since 2021/6/22/20:49
 */
public class RabbitmqUtil {
    /**
     * 获取连接
     *
     * @return
     */
    public static Connection getConnection() {
        //创建一个连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //工厂ip连接RabbitMQ的队列
        connectionFactory.setHost("127.0.0.1");
        //用户名
        connectionFactory.setUsername("guest");
        //密码
        connectionFactory.setPassword("guest");
        //创建连接
        Connection connection = null;
        try {
            connection = connectionFactory.newConnection();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 获取信道
     * @return
     */
    public static Channel getChannel() {
        try {
            return getConnection().createChannel();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
